package com.ruan.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruan.common.Code;
import com.ruan.common.Result;
import com.ruan.exception.BusinessException;
import com.ruan.mapper.AdminMapper;
import com.ruan.mapper.EmployeeMapper;
import com.ruan.mapper.OverTimeMapper;
import com.ruan.pojo.*;
import com.ruan.pojoExt.OverTimeExt;
import com.ruan.pojoExt.ResignExt;
import com.ruan.service.DepartmentService;
import com.ruan.service.EmployeeService;
import com.ruan.service.OverTimeService;
import com.ruan.utils.DateUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;

import javax.annotation.Resource;
import java.sql.Date;
import java.sql.Time;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
@Transactional
public class OverTimeServiceImpl extends ServiceImpl<OverTimeMapper, OverTime> implements OverTimeService {

    @Autowired
    private OverTimeMapper overTimeMapper;

    @Autowired
    private DepartmentService departmentService;

    @Autowired
    private EmployeeService employeeService;

    @Autowired
    private OverTimeService overTimeService;

    @Autowired
    private AdminMapper adminMapper;

    @Autowired
    private EmployeeMapper employeeMapper;


    /**
     * 通过加班上报
     * @param id 员工id
     * @param adminId 管理员id
     */
    @Override
    public void agreed(Integer id,Integer adminId) {
        OverTime overTime = overTimeMapper.selectById(id);
        overTime.setStatus("通过");
        Admin admin = adminMapper.selectById(adminId);
        overTime.setApprover(admin.getUsername());
        overTimeMapper.updateById(overTime);
    }

    /**
     * 驳回加班上报
     * @param id 员工id
     * @param adminId 管理员id
     */
    @Override
    public void refuse(Integer id,Integer adminId) {
        OverTime overTime = overTimeMapper.selectById(id);
        overTime.setStatus("驳回");
        Admin admin = adminMapper.selectById(adminId);
        overTime.setApprover(admin.getUsername());
        overTimeMapper.updateById(overTime);
    }

    /**
     * 上报加班记录
     * @param overTime 加班记录对象
     */
    @Override
    public void submitOverTime(OverTime overTime) {
//        System.out.println("submitOverTime传来的参数是："+overTime);
        Date today = new Date(System.currentTimeMillis());
        Date day = overTime.getOverTimeDate();

        if (day.after(today)){//防止出现未来时间
            throw new BusinessException(Code.SAVE_ERR,"只能申请当天加班，请重新输入！");
        }

        LambdaQueryWrapper<OverTime> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(OverTime::getOverTimeDate,day);
        queryWrapper.eq(OverTime::getEmployeeId,overTime.getEmployeeId());
//        queryWrapper.eq(OverTime::getPhone,overTime.getPhone());
        OverTime overTimeQuery = overTimeMapper.selectOne(queryWrapper);
        System.out.println("当天已经有加班记录了吗："+overTimeQuery);
        if (overTimeQuery!=null){
            throw new BusinessException(Code.SAVE_ERR,"该日期已经上报加班记录！");
        }

        overTime.setSubmitTime(new java.util.Date());
        Time endTime = overTime.getEndTime();
        Time startTime = overTime.getStartTime();
        String[] endStrings = endTime.toString().split(":");
        String[] startStrings = startTime.toString().split(":");
        int end = Integer.valueOf(endStrings[0]);
        int start = Integer.valueOf(startStrings[0]);
        int hours = end-start;
        overTime.setHours(hours);
        overTime.setStatus("待审核");
        overTimeMapper.insert(overTime);
    }

    /**
     * 分页+条件 员工获取个人加班记录
     * @param currentPage 当前页
     * @param pageSize 每页最大数
     * @param querySelfAttendanceCondition 查询条件
     * @return
     */
    @Override
    public IPage<OverTime> getSelfOverTime(int currentPage, int pageSize,
                                           QuerySelfAttendanceCondition querySelfAttendanceCondition) {
        Date month = querySelfAttendanceCondition.getMonth();//2023-2-1
        IPage<OverTime> iPage=new Page<>(currentPage,pageSize);
        QueryWrapper<OverTime> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(querySelfAttendanceCondition.getStatus()!=null,"status", querySelfAttendanceCondition.getStatus());

        LambdaQueryWrapper<Employee> queryWrapper1 = new LambdaQueryWrapper<>();
        queryWrapper1.eq(Employee::getPhone,querySelfAttendanceCondition.getPhone());
        Employee employee = employeeMapper.selectOne(queryWrapper1);
        queryWrapper.eq("employee_id", employee.getId());
        //        queryWrapper.like("phone", querySelfAttendanceCondition.getPhone());
        if (month!=null){
            String firstDay = month.toString();//每月的第一天
            String lastDay = DateUtils.getLastDay(firstDay);//每月最后一天

            queryWrapper.between("submit_time",firstDay,lastDay);

            return overTimeMapper.selectPage(iPage,queryWrapper);
        }else {

            return overTimeMapper.selectPage(iPage,queryWrapper);
        }
    }

    /**
     * 员工撤销加班记录上报
     * @param id
     */
    @Override
    public void cancelLeave(Integer id) {
        overTimeMapper.deleteById(id);
    }
}
